using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.FileIO;
using System.IO;
using System.Collections;
using System.Diagnostics;

namespace Windows_Explorer.Forms
{
    public partial class frmProperties : Form
    {
        public frmProperties()
        {
            InitializeComponent();
        }

        private void frmProperties_Load(object sender, EventArgs e)
        {
            #region "Set Help Strings"
            HelpProvider hp = new HelpProvider();
            hp.SetHelpString(lb, "Displays how many files and folders are selected\nwhen you display the properties.");
            hp.SetHelpString(txtType, "Displays the type of file or folder that is selected.");
            hp.SetHelpString(lbType, "Displays the type of file or folder that is selected.");
            hp.SetHelpString(lbLocation, "Displays the location of the file or folder.");
            hp.SetHelpString(txtLocation, "Displays the location of the file or folder.");
            hp.SetHelpString(lbSize, "Displays the size of the file or folder.");
            hp.SetHelpString(txtSize, "Displays the size of the file or folder.");
            hp.SetHelpString(lbAttribute, "Help is available for each item in this group. Click [?] at the top\nof the dialog box, and then click the item you want information\nabout.");
            hp.SetHelpString(cbReadOnly, "Specifies whether this file is read-only, which means that it\ncannot be changed or accidentally deleted.");
            hp.SetHelpString(cbHidden, "Specifies whether the file or folder is hidden, which means that\nyou cannot see or use it unless you know its name.");
            hp.SetHelpString(cbArchive, "Specifies whether this file or folder should be archived. Some\nprograms use this option to control which files and folders are\nbacked up. ");
            hp.SetHelpString(btnApply, "Saves all the changes you have made without closing the\ndialog box.");
            hp.SetHelpString(btnCancel, "Closes the dialog box without saving any changes you have\nmade.");
            hp.SetHelpString(btnOK, "Closes the dialog box and saves any changes you\nhave made.");
            hp.SetHelpString(txtName, "Specifies the name of the file or folder, and provides a space\nfor you to type a new name.");
            hp.SetHelpString(txtCreated, "Displays the date that the file or folder was created.");
            hp.SetHelpString(lbCreated, "Displays the date that the file or folder was created.");
            hp.SetHelpString(txtContain, "Displays the number of files and folders contained in\nthis folder.");
            hp.SetHelpString(lbContains, "Displays the number of files and folders contained in\nthis folder.");
            #endregion
            txtContain.Text = "";
            txtCreated.Text = "";
            txtLocation.Text = "";
            txtName.Text = "";
            txtSize.Text = "";
            txtType.Text = "";
            btnApply.Enabled = false; ;
            if (!Program.fmain.isMultiItems)
            {
                try
                {
                    lb.Hide();
                    txtName.Show();
                    txtContain.Show();
                    lbContains.Show();
                    lbCreated.Show();
                    txtCreated.Show();
                    cbArchive.Show();
                    cbHidden.Show();
                    cbReadOnly.Show();
                    lbAttribute.Show();
                    ListViewItem lvi = new ListViewItem();
                    lvi = Program.fmain.listviewitem1;
                    if (lvi != null)
                    {
                        Program.fproperties.Text = lvi.SubItems[0].Text.ToString() + " Properties";
                        string name = lvi.SubItems[0].Text.ToString();
                        if (name.Contains("."))
                        {
                            int i = name.IndexOf('.');
                            if (i != -1) txtName.Text = name.Substring(0, i).ToString();
                        }
                        else txtName.Text = lvi.SubItems[0].Text.ToString();
                        txtType.Text = lvi.SubItems[1].Text.ToString();
                        if ((txtType.Text != "File Folder") && (txtType.Text != "Fixed")) txtType.Text = txtType.Text.Trim().Substring(1, txtType.Text.Length - 1).ToString() + " File";
                        string[] locationarr = lvi.SubItems[4].Text.ToString().Split('\\');
                        txtLocation.Text = lvi.SubItems[4].Text.ToString().Replace(locationarr[locationarr.Length - 1], "");
                        DirectoryInfo di = new DirectoryInfo(lvi.SubItems[4].Text.ToString());
                        txtContain.Text = "";
                        if (di.Exists)
                        {
                            Class.clsItem.Contains(di);
                            txtContain.Text = Class.clsItem.fileCount.ToString() + " Files, " + Class.clsItem.folderCount.ToString() + " Folders";
                            Class.clsItem.fileCount = 0;
                            Class.clsItem.folderCount = 0;
                            string s = di.Attributes.ToString();
                            if (di.Attributes.ToString().Contains("Archive")) cbArchive.Checked = true;
                            else cbArchive.Checked = false;
                            if (di.Attributes.ToString().Contains("ReadOnly")) cbReadOnly.Checked = true;
                            else cbReadOnly.Checked = false;
                            if (di.Attributes.ToString().Contains("Hidden")) cbHidden.Checked = true;
                            else cbHidden.Checked = false;
                        }
                        else
                        {
                            txtContain.Text = "";
                            FileInfo fi = new FileInfo(lvi.SubItems[4].Text.ToString());
                            if (fi.Exists)
                            {
                                if (fi.Attributes.ToString().Contains("Archive")) cbArchive.Checked = true;
                                else cbArchive.Checked = false;
                                if (fi.Attributes.ToString().Contains("ReadOnly")) cbReadOnly.Checked = true;
                                else cbReadOnly.Checked = false;
                                if (fi.Attributes.ToString().Contains("Hidden")) cbHidden.Checked = true;
                                else cbHidden.Checked = false;
                            }
                        }
                        if (lvi.SubItems[1].Text != "File Folder")
                        {
                            txtSize.Text = lvi.SubItems[2].Text.ToString();
                        }
                        else//File Folder
                        {
                            long sizeKB = Class.clsItem.folderSize / 1024;
                            long sizeMB = sizeKB / 1024;
                            long sizeGB = sizeMB / 1024;
                            if (sizeKB < 1000)
                                txtSize.Text = sizeKB.ToString() + " KB (" + Class.clsItem.folderSize.ToString() + " bytes)";
                            else if (sizeMB < 1000) txtSize.Text = sizeMB.ToString() + "." + (sizeKB - sizeMB * 1024).ToString() + " MB (" + Class.clsItem.folderSize.ToString() + " bytes)";
                            else txtSize.Text = sizeGB.ToString() + "." + (sizeMB - sizeGB * 1024).ToString() + " GB (" + Class.clsItem.folderSize.ToString() + " bytes)";
                            Class.clsItem.folderSize = 0;
                        }
                        txtCreated.Text = di.LastWriteTime.ToString();
                    }
                }
                catch { }
            }
            else//nhieu item
            {
                ListView.SelectedListViewItemCollection slic = Program.fmain.slic;
                this.Text = slic[0].Text + ",... Properties";
                txtContain.Hide();
                cbArchive.Hide();
                cbHidden.Hide();
                cbReadOnly.Hide();
                lbAttribute.Hide();
                lbContains.Hide();
                lbCreated.Hide();
                txtCreated.Hide();
                lb.Show();
                Point pt = txtName.Location;
                txtName.Hide();
                int fileCount = 0, folderCount = 0;
                long folderSize = 0;
                foreach (ListViewItem lvi in slic)
                {
                    string path = lvi.SubItems[4].Text;
                    FileInfo fi = new FileInfo(path);
                    if (fi.Exists)
                    {
                        fileCount++;
                        folderSize += fi.Length;
                    }
                    DirectoryInfo di = new DirectoryInfo(path);
                    if (di.Exists)
                    {
                        Class.clsItem.Contains(di);
                        fileCount += Class.clsItem.fileCount;
                        folderCount += Class.clsItem.folderCount;
                        folderSize += Class.clsItem.folderSize;
                        Class.clsItem.folderSize = 0;
                        Class.clsItem.folderCount = 0;
                        Class.clsItem.fileCount = 0;
                    }
                }
                lb.Text = fileCount.ToString() + " Files, " + folderCount.ToString() + " Folders";
                Class.clsItem.folderSize = 0;
                Class.clsItem.folderCount = 0;
                Class.clsItem.fileCount = 0;
                txtLocation.Text = "All in " + slic[0].SubItems[5].Text;
                long sizeKB = folderSize / 1024;
                long sizeMB = sizeKB / 1024;
                long sizeGB = sizeMB / 1024;
                if (sizeKB < 1000)
                    txtSize.Text = sizeKB.ToString() + " KB (" + folderSize.ToString() + " bytes)";
                else if (sizeMB < 1000) txtSize.Text = sizeMB.ToString() + "." + (sizeKB - sizeMB * 1024).ToString() + " MB (" + folderSize.ToString() + " bytes)";
                else txtSize.Text = sizeGB.ToString() + "." + (sizeMB - sizeGB * 1024).ToString() + " GB (" + folderSize.ToString() + " bytes)";
                txtType.Text = "Multiple Types";

            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            try
            {
                if (!Program.fmain.isMultiItems)
                {
                    string name = Program.fmain.listviewitem1.SubItems[0].Text.ToString();
                    DirectoryInfo di = new DirectoryInfo(Program.fmain.listviewitem1.SubItems[4].Text.ToString());
                    FileInfo fi = new FileInfo(Program.fmain.listviewitem1.SubItems[4].Text.ToString());
                    if ((di.Exists && Program.fmain.listviewitem1.SubItems[2].Text.ToString() != "Fixel") | (fi.Exists))
                    {
                        if (di.Exists)//folder
                        {
                            if (cbArchive.Checked) di.Attributes |= FileAttributes.Archive;
                            if (cbHidden.Checked) di.Attributes |= FileAttributes.Hidden;
                            if (cbReadOnly.Checked) di.Attributes |= FileAttributes.ReadOnly;
                            if (!cbArchive.Checked && (di.Attributes.ToString().Contains("Archive")))
                                di.Attributes ^= FileAttributes.Archive;
                            if (!cbHidden.Checked && (di.Attributes.ToString().Contains("Hidden")))
                                di.Attributes ^= FileAttributes.Hidden;
                            if (!cbReadOnly.Checked && (di.Attributes.ToString().Contains("ReadOnly")))
                                di.Attributes ^= FileAttributes.ReadOnly;
                            if (di.Attributes.ToString().Contains("Hidden")) Program.fmain.listviewitem1.ImageKey = "Hidden";
                            else Program.fmain.listviewitem1.ImageKey = "Folder";
                            if (txtName.Text.Trim() != name.Trim())
                            {
                                DirectoryInfo diNew = di.Parent;
                                foreach (DirectoryInfo d in diNew.GetDirectories())
                                {
                                    if (d.Name.Trim().ToString() == txtName.Text.Trim().ToString())
                                    {
                                        MessageBox.Show("Tên thư mục này đã tồn tại!\nVui lòng chọn tên khác", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                        txtName.Text = name;
                                        return;
                                    }
                                }
                                try
                                {
                                    Microsoft.VisualBasic.FileIO.FileSystem.
                                        RenameDirectory(Program.fmain.listviewitem1.SubItems[4].Text.ToString(), txtName.Text.Trim());
                                    Program.fmain.lvListView.FocusedItem.SubItems[0].Text = txtName.Text.Trim();
                                }
                                catch { }
                            }
                        }
                        else//file
                        {
                            if (cbArchive.Checked) fi.Attributes |= FileAttributes.Archive;
                            if (cbHidden.Checked) fi.Attributes |= FileAttributes.Hidden;
                            if (cbReadOnly.Checked) fi.Attributes |= FileAttributes.ReadOnly;
                            if (!cbArchive.Checked && (fi.Attributes.ToString().Contains("Archive")))
                                fi.Attributes ^= FileAttributes.Archive;
                            if (!cbHidden.Checked && (fi.Attributes.ToString().Contains("Hidden")))
                                fi.Attributes ^= FileAttributes.Hidden;
                            if (!cbReadOnly.Checked && (fi.Attributes.ToString().Contains("ReadOnly")))
                                fi.Attributes ^= FileAttributes.ReadOnly;
                            if (txtName.Text.Trim() != name.Trim())
                            {
                                DirectoryInfo diNew = fi.Directory;
                                foreach (FileInfo f in diNew.GetFiles())
                                {
                                    if (f.Name.Trim().ToString() == txtName.Text.Trim().ToString())
                                    {
                                        MessageBox.Show("Tên file này đã tồn tại!\nVui lòng chọn tên khác", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                                        txtName.Text = name;
                                        return;
                                    }
                                }
                                if (Program.fmain.listviewitem1.SubItems[0].Text.ToString() != (txtName.Text.Trim() + fi.Extension))
                                {
                                    Microsoft.VisualBasic.FileIO.FileSystem.
                                    RenameFile(Program.fmain.listviewitem1.SubItems[4].Text.ToString(), txtName.Text.Trim() + fi.Extension);
                                    Program.fmain.lvListView.FocusedItem.SubItems[0].Text = txtName.Text.Trim();
                                }
                            }
                        }
                    }
                    btnApply.Enabled = false;
                }
            }
            catch { }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            btnApply.PerformClick();
            this.Close();
        }

        private void cbReadOnly_CheckedChanged(object sender, EventArgs e)
        {
            btnApply.Enabled = true;
        }

        private void txtName_TextChanged(object sender, EventArgs e)
        {
            if (Program.fmain.listviewitem1.SubItems[0].Text.Trim().ToString().ToUpper() !=
           (txtName.Text.Trim().ToString().ToUpper() +
           Program.fmain.listviewitem1.SubItems[1].Text.Trim().ToString().ToUpper())
           && Program.fmain.listviewitem1.SubItems[1].Text != "File Folder")
                btnApply.Enabled = true;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string path = "C:\\Documents and Settings\\All Users\\Documents";
            DirectoryInfo di = new DirectoryInfo(path);
            if (di.Exists)
                Process.Start(path);
        }
        
    }
}
